package com.xiaozhu.common.toggle;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.xiaozhu.common.R;
import com.xiaozhu.library.activity.BaseActivity;
import com.xiaozhu.library.adapters.MyFragmentPagerAdapter;
import com.xiaozhu.library.entity.EventBusEntity;
import com.xiaozhu.library.utils.LogUtil;
import com.xiaozhu.library.widget.custom.SwitchButton;
import com.xiaozhu.library.widget.custom.TitleBarView;

import java.util.ArrayList;

/**
 * @说明 切换按钮
 * @作者 LY
 * @时间 2017/12/21 14:17
 * @邮箱 2743569843@qq.com
 * @版权 Copyright(c) 2017 LY-版权所有
 * @备注
 */
public class SwitchButtonActivity extends BaseActivity {
    private TitleBarView titleBar;
    private SwitchButton switchButton;
    private ViewPager viewPage;
    private String[] barNames = {"主页", "咨询", "个人"};
    private ArrayList<Fragment> fragmentsList;

    @Override
    public void initView() {
        titleBar = this.findViewById(R.id.titleBar);
        switchButton = this.findViewById(R.id.switchButton);
        viewPage = this.findViewById(R.id.viewPage);

        titleBar.setTitle("切换按钮");
        titleBar.setBtnLeft(R.mipmap.icon_back, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        fragmentsList = new ArrayList<>();
        for (int i = 0; i < barNames.length; i++) {
            fragmentsList.add(SwitchFragment.newInstance(barNames[i]));
        }
        viewPage.setOffscreenPageLimit(fragmentsList.size());
        viewPage.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList));
        viewPage.setCurrentItem(0);
        viewPage.setOnPageChangeListener(new MyOnPageChangeListener());
        switchButton.setText(barNames).setOnSwitchListener(new SwitchButton.OnSwitchListener() {
            @Override
            public void onSwitch(int position, String tabText) {
                viewPage.setCurrentItem(position);
                LogUtil.i("=====>>>" + position + "   " + tabText);
            }
        }).setSelectedTab(0);

    }

    @Override
    public void business() {

    }

    @Override
    public int getLayoutResID() {
        return R.layout.activity_switch_button;
    }

    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageSelected(int arg0) {
            switchButton.setSelectedTab(arg0);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }
    }
}
